
package com.borqs.music.ui.adapter;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import java.util.List;

import android.content.Context;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.borqs.music.R;
import com.borqs.music.model.SongInfo;

public class OnlineTrackListAdapter extends ArrayAdapter<SongInfo> {
    private static final int TYPE_COUNT = 2;

    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_STATUS = 1;

    public static enum Status {
        NONE, MORE, FAILED, LOADING;
    }

    private final String mUnknownArtist;

    private static class ViewHolder {
        public TextView line1;
        public TextView line2;
    }

    private static class StatusViewHolder {
        public TextView text;
        public ProgressBar progress;
    }

    private static final int layoutId = R.layout.track_list_item;
    private static final int statusId = R.layout.list_load_foot;

    private LayoutInflater inflater;
    private boolean hasFooter = false;

    public OnlineTrackListAdapter(Context context, List<SongInfo> items) {
        super(context, 0, items);
        inflater = LayoutInflater.from(context);
        mUnknownArtist = context.getString(R.string.unknown_artist_name);
    }

    @Override
    public int getCount() {
        return super.getCount() + (hasFooter ? 1 : 0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int viewType = getItemViewType(position);

        if (viewType == TYPE_NORMAL) {
            View returnView = null;
            ViewHolder vh = null;
            if (null == convertView) {
                returnView = inflater.inflate(layoutId, parent, false);
                vh = new ViewHolder();
                vh.line1 = (TextView) returnView.findViewById(R.id.line1);
                vh.line2 = (TextView) returnView.findViewById(R.id.line2);
                returnView.setTag(vh);
            } else {
                returnView = convertView;
                vh = (ViewHolder) returnView.getTag();
            }

            SongInfo song = getItem(position);
            vh.line1.setText(song.title);

            if (TextUtils.isEmpty(song.artist) || song.artist.equals(MediaStore.UNKNOWN_STRING)) {
                vh.line2.setText(mUnknownArtist);
            } else {
                vh.line2.setText(song.artist);
            }
            
            return returnView;
        }

        if (viewType == TYPE_STATUS) {
            View returnView = null;
            StatusViewHolder vh;
            if (convertView == null) {
                returnView = inflater.inflate(statusId, parent, false);
                vh = new StatusViewHolder();
                vh.text = (TextView) returnView.findViewById(R.id.text_load_next_page);
                vh.progress = (ProgressBar) returnView.findViewById(R.id.list_foot_progress);
                returnView.setTag(vh);
            } else {
                returnView = convertView;
                vh = (StatusViewHolder) returnView.getTag();
            }
            Status status = mCurrentStatus;
            vh.progress.setVisibility(status == Status.LOADING ? VISIBLE : GONE);
            if (status == Status.MORE) {
                vh.text.setText(R.string.text_load_next_page);
            } else if (status == Status.LOADING) {
                vh.text.setText(R.string.text_loading_song);
            } else if (status == Status.FAILED) {
                vh.text.setText(R.string.text_load_failed);
            } else {
                throw new IllegalStateException("Illegal status at " + position + ", status = " + status);
            }

            return returnView;
        }

        throw new IllegalStateException("Illegal view type at " + position + ", type = " + viewType);
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == super.getCount()) {
            return TYPE_STATUS;
        }

        return TYPE_NORMAL;
    }

    private Status mCurrentStatus;

    public Status getStatus() {
        return mCurrentStatus;
    }

    public void setStatus(Status status) {
        mCurrentStatus = status;
        hasFooter = (status != Status.NONE);
    }
}
